﻿using CloudManager.ThirdPartyProviders;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Windows.Forms;

namespace CloudManager
{
    public partial class FrmCloudManager : Form
    {
        private CloudService SelectedService { get; set; }

        public FrmCloudManager()
        {
            InitializeComponent();

            pnlService.MouseEnter += pnlService_MouseEnter;
            lBServices.MouseEnter += lBServices_MouseEnter;

            var assembly = Assembly.GetExecutingAssembly();
            var assemblyName = assembly.GetName();
            linkVersion.Text = "Version " + assemblyName.Version.ToString() + " sourcecode @ CodePlex.";

            linkVersion.LinkArea = new LinkArea(linkVersion.Text.LastIndexOf(" ") + 1, 8);

            try
            {
                if (DropboxProvider.IsInstalled())
                {
                    cmbService.Items.Add("Dropbox");
                }
                if (GoogleDriveProvider.IsInstalled())
                {
                    cmbService.Items.Add("Google Drive");
                }
                if (cmbService.Items.Count == 0)
                {
                    cmbService.Items.Add("No services installed");
                    btnAddService.Enabled = false;
                }
                LoadServices();
            }
            catch (Exception)
            {
                MessageBox.Show(this, "Unable to load the services, the application can not start.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.Close();
            }
        }

        private void LoadServices()
        {
            lBServices.Items.Clear();
            var services = CloudService.GetServices();

            if (services != null)
            {

                foreach (var cs in services)
                {
                    lBServices.Items.Add(cs);
                }
            }

            ClearCloudService();
        }

        void lBServices_MouseEnter(object sender, EventArgs e)
        {
            if (!pnlService.Controls.Cast<Control>().Contains(this.ActiveControl))
            {
                lBServices.Select();
                this.AcceptButton = btnAdd;
            }
        }

        void pnlService_MouseEnter(object sender, EventArgs e)
        {
            if (!pnlService.Controls.Cast<Control>().Contains(this.ActiveControl))
            {
                pnlService.Select();
            }

            this.AcceptButton = btnSave;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var cs = SelectedService ?? new CloudService();

            cs.DisplayName = txtDisplayName.Text;
            cs.Description = txtDescription.Text;
            cs.AccountUrl = txtAccountUrl.Text;
            cs.InformationUrl = txtInfoUrl.Text;
            cs.LocalFolder = txtLocalFolder.Text;
            cs.Url16x16 = txt16ImgUrl.Text;
            cs.Url20x20 = txt20ImgUrl.Text;
            cs.Url24x24 = txt24ImgUrl.Text;
            cs.Url32x32 = txt32ImgUrl.Text;
            cs.Url40x40 = txt40ImgUrl.Text;
            cs.Url48x48 = txt48ImgUrl.Text;

            try
            {
                cs.Save();

                LoadServices();

            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnBrowseLocalFolder_Click(object sender, EventArgs e)
        {
            var fbd = new FolderBrowserDialog { SelectedPath = txtLocalFolder.Text, Description = "Local folder" };

            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                txtLocalFolder.Text = fbd.SelectedPath;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            lBServices.SelectedIndex = -1;

            pnlService.BringToFront();
            btnSave.Visible = true;
            btnBack.Visible = true;

        }

        private void lBServices_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lBServices.SelectedIndex != -1)
            {
                OpenCloudService(lBServices.SelectedItem as CloudService);
            }
            else
            {
                ClearCloudService();
            }
        }

        private void OpenCloudService(CloudService service)
        {
            SelectedService = service;

            txtDisplayName.Text = service.DisplayName;
            txtDescription.Text = service.Description;
            txtInfoUrl.Text = service.InformationUrl;
            txtAccountUrl.Text = service.AccountUrl;
            txt48ImgUrl.Text = service.Url48x48;
            txt40ImgUrl.Text = service.Url40x40;
            txt32ImgUrl.Text = service.Url32x32;
            txt24ImgUrl.Text = service.Url24x24;
            txt20ImgUrl.Text = service.Url20x20;
            txt16ImgUrl.Text = service.Url16x16;
            txtLocalFolder.Text = service.LocalFolder;
            btnRemove.Enabled = true;

            pnlService.BringToFront();
            btnSave.Visible = true;
            btnBack.Visible = true;
        }

        private void ClearCloudService()
        {
            SelectedService = null;
            lBServices.SelectedIndex = -1;

            txtDisplayName.Text = "";
            txtDescription.Text = "";
            txtInfoUrl.Text = "";
            txtAccountUrl.Text = "";
            txt48ImgUrl.Text = "";
            txt40ImgUrl.Text = "";
            txt32ImgUrl.Text = "";
            txt24ImgUrl.Text = "";
            txt20ImgUrl.Text = "";
            txt16ImgUrl.Text = "";
            txtLocalFolder.Text = "";
            btnRemove.Enabled = false;

            btnSave.Visible = false;
            pnlAddService.BringToFront();
            btnBack.Visible = false;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Do you want to remove \"" + SelectedService.DisplayName + "\"?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    SelectedService.Remove();

                    LoadServices();
                }
                catch (Exception ex)
                {
                    if (ex is SecurityException || ex is UnauthorizedAccessException)
                        MessageBox.Show(this, "You do not have sufficient rights to remove the service.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else
                        MessageBox.Show(this, "The following error occured:" + Environment.NewLine + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnAddService_Click(object sender, EventArgs e)
        {
            try
            {

                switch (cmbService.Text)
                {
                    case "Dropbox": //Dropbox
                        new DropboxProvider().Save();
                        break;
                    case "Google Drive": //Google Drive
                        new GoogleDriveProvider().Save();
                        break;
                }

                LoadServices();

            }
            catch (Exception ex)
            {
                if (ex is SecurityException || ex is UnauthorizedAccessException)
                    MessageBox.Show(this, "You do not have sufficient rights to save the service.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(this, "The following error occured:" + Environment.NewLine + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            ClearCloudService();
        }

        private void linkVersion_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("https://cloudmanageroffice.codeplex.com/");
        }

        private void FrmCloudManager_Load(object sender, EventArgs e)
        {

        }

    }
}
